<?php

namespace Drupal\shushengyuan\Schemas;

/**
 * @OA\Schema(
 *   schema="CategoryItemGroup",
 *   type="object",
 *   description="CategoryItemGroup"
 * )
 */
class CategoryItemGroup extends AbstractBaseModel {

  protected function getEntityType() {
    return FALSE;
  }

  protected function setValue() {
    [$id, $name] = $this->context;
    $value = [
      /**
       * @OA\Property(
       *   property="id",
       *   type="integer",
       *   description="ID",
       * ),
       */
      'id' => (int) $id,
      /**
       * @OA\Property(
       *   property="name",
       *   type="string",
       *   description="分组名称",
       * ),
       */
      'name' => (string) $name,
      /**
       * @OA\Property(
       *   property="list",
       *   type="array",
       *   description="包含的分类",
       *   @OA\Items(ref="#/components/schemas/CategoryItemIcon")
       * ),
       */
      'list' => $this->getList($id),
    ];
    $this->merge($value);
  }

  protected function getList(int $id) {
    $children = db_query('SELECT id, name, weight, parent, status
                      FROM shusy_category
                      WHERE (parent = :parent) AND (status = 1)
                      ORDER BY weight ASC', [':parent' => $id]);
    $list = [];
    foreach ($children as $child) {
      $list[] = CategoryItemIcon::get($child->id);
    }
    return $list;
  }
}
